package cn.iocoder.yudao.module.def.controller.admin.defcommon;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;

@Tag(name = "模版下载")
@RestController
@RequestMapping("/def/download")
@Validated
@AllArgsConstructor
public class FileDownloadService {

    private static final String TEMPLATE_PATH = "static/excel/template.xlsx"; // 配置化路径

    private static final String TEMPLATE_PATH2 = "static/excel/gcjd.xlsx"; // 配置化路径

    private static final String TEMPLATE_PATH3 = "static/excel/MOBAN1.xls"; // 配置化路径

    private static final String TEMPLATE_PATH10 = "static/excel/devicenum.xlsx"; // 配置化路径
    private static final String FILE_NAME = "template.xlsx"; // 配置化文件名

    @GetMapping("/tmp1")
    public ResponseEntity<byte[]> downloadExcelTemplate() {
        try (InputStream inputStream = new ClassPathResource(TEMPLATE_PATH).getInputStream()) {
            // 使用 StreamUtils 将 InputStream 转换为字节数组
            byte[] fileContent = StreamUtils.copyToByteArray(inputStream);

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", FILE_NAME);
            // 返回文件内容
            return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
        } catch (IOException e) {
            // 记录异常日志
            e.printStackTrace();

            // 返回 500 错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @GetMapping("/tmp2")
    public ResponseEntity<byte[]> downloadExcelTemplate2() {
        try (InputStream inputStream = new ClassPathResource(TEMPLATE_PATH2).getInputStream()) {
            // 使用 StreamUtils 将 InputStream 转换为字节数组
            byte[] fileContent = StreamUtils.copyToByteArray(inputStream);

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", FILE_NAME);
            // 返回文件内容
            return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
        } catch (IOException e) {
            // 记录异常日志
            e.printStackTrace();

            // 返回 500 错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @GetMapping("/tmp10")
    public ResponseEntity<byte[]> downloadExcelTemplate10() {
        try (InputStream inputStream = new ClassPathResource(TEMPLATE_PATH10).getInputStream()) {
            // 使用 StreamUtils 将 InputStream 转换为字节数组
            byte[] fileContent = StreamUtils.copyToByteArray(inputStream);

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", FILE_NAME);
            // 返回文件内容
            return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
        } catch (IOException e) {
            // 记录异常日志
            e.printStackTrace();

            // 返回 500 错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @GetMapping("/tmp3")
    public ResponseEntity<byte[]> downloadExcelTemplate3() {
        try (InputStream inputStream = new ClassPathResource(TEMPLATE_PATH3).getInputStream()) {
            // 使用 StreamUtils 将 InputStream 转换为字节数组
            byte[] fileContent = StreamUtils.copyToByteArray(inputStream);

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", FILE_NAME);
            // 返回文件内容
            return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
        } catch (IOException e) {
            // 记录异常日志
            e.printStackTrace();

            // 返回 500 错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}
